package cn.xfdzx.android.apps.shop.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.myshop.MyShopCenterActivity;
import cn.xfdzx.android.apps.shop.activity.myshop.ShopMyAccountActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.ShopInDetailsBeen;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

public class ShopmMyFragment extends BaseMvpFragment implements View.OnClickListener {

    RelativeLayout rl_account;//账户资金
    RelativeLayout rl_shopmanager;//门店管理
    RelativeLayout rl_customerservice;//在线客服
    RelativeLayout rl_set;//设置
    ImageView iv_shop;//店铺头像
    RelativeLayout rl_titlebg;//头部背景
    TextView tv_shop_name;//店铺名称
    private Dialog dialog;

    @Override
    protected void initView(View view) {
        rl_account = view.findViewById(R.id.rl_account);
        rl_shopmanager = view.findViewById(R.id.rl_shopmanager);
        rl_customerservice = view.findViewById(R.id.rl_customerservice);
        rl_set = view.findViewById(R.id.rl_set);
        iv_shop = view.findViewById(R.id.iv_shop);
        rl_titlebg = view.findViewById(R.id.rl_titlebg);
        tv_shop_name = view.findViewById(R.id.tv_shop_name);

        rl_account.setOnClickListener(this);
        rl_shopmanager.setOnClickListener(this);
        rl_customerservice.setOnClickListener(this);
        rl_set.setOnClickListener(this);
        setZhi();

        dialog = Utils.addDialog(getActivity(), R.layout.dialog, R.string.are_you_sure_you_want_callcustomer, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                callPhone("15810103429");
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop_my;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    public static ShopmMyFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        ShopmMyFragment fragment = new ShopmMyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_account://账户资金
                Intent intent = new Intent(getActivity(), ShopMyAccountActivity.class);
                if (((MyShopCenterActivity) getActivity()).beans.getData().getSales_model() == 0) {
                   intent.putExtra("type",0);
                } else {
                    intent.putExtra("type",1);
                }
                startActivity(intent);
                break;
            case R.id.rl_shopmanager://门店管理

                break;
            case R.id.rl_customerservice://在线客服
                dialog.show();
                break;
            case R.id.rl_set://设置
                break;

        }
    }


    private void setZhi() {
        MyShopCenterActivity myShopCenterActivity = (MyShopCenterActivity) getActivity();
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
//            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);

        if (EmptyUtil.isNotEmpty(myShopCenterActivity.beans)){
            Glide.with(getActivity()).load(myShopCenterActivity.beans.getData().getLogo()).apply(options.transform(new CircleCrop(), roundedCorners)).into(iv_shop);
            tv_shop_name.setText(myShopCenterActivity.beans.getData().getName());
            if (myShopCenterActivity.beans.getData().getSales_model() == 0) {
                rl_titlebg.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            } else {
                rl_titlebg.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            }
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
